/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.click.app.webapp.reportObjects;


import com.click.app.model.LaundryItem;
import com.click.app.model.LaundryRequestItem;
import com.click.app.model.ModelSize;
import com.click.app.service.LaundryItemManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author Joel Chuca Condo
 * @date 05/07/2009
 */

public class LaundryFailReceptionTable {

    private List<String> head;
    private List<BodyRow> body;
    private List<LaundryRequestItem> laundryRequestItems;
    private LaundryItemManager laundryItemManager;
    private Set<ModelSize> modelSizes;
    private boolean correct;


    public LaundryFailReceptionTable(List<LaundryRequestItem> laundryRequestItems, LaundryItemManager laundryItemManager, Set<ModelSize> modelSizes) {
        this.laundryRequestItems = laundryRequestItems;
        this.laundryItemManager = laundryItemManager;
        this.modelSizes = modelSizes;
        head = new ArrayList<String>();
        body = new ArrayList<BodyRow>();
        correct = false;
        init();
    }

    public List<BodyRow> getBody() {
        return body;
    }

    public List<String> getHead() {
        return head;
    }

    public boolean isCorrect() {
        return correct;
    }


    private void init() {
        
        head.add("T. Lavado");
        head.add("Marca");

        if( modelSizes.isEmpty() ){
            System.out.println("modelSizes List is empty");
            correct = false;
            return;
        }

        List<Double> totalQuantitiesHor = new ArrayList<Double>();
        for (ModelSize modelSize : modelSizes) {
            head.add(modelSize.getValueSize());
            totalQuantitiesHor.add(0D);
        }
        head.add("Total");
        totalQuantitiesHor.add(0D);


        if (laundryRequestItems != null) {
            if (laundryRequestItems.size() > 0) {

                BodyRow row = new BodyRow();
                BodyRow rowTotal = new BodyRow(modelSizes.size() + 3);

                rowTotal.add(0,"---");
                rowTotal.add(1,"---");
                for (LaundryRequestItem laundryRequestItem : laundryRequestItems) {

                    row.add(laundryRequestItem.getLaundryType().getType());
                    row.add(laundryRequestItem.getTradeMark().getName());

                    List<LaundryItem> laundryItems = laundryItemManager.findByOrderSize(laundryRequestItem.getId());
                    int indexModelSize = 0;
                    for (ModelSize modelSize : modelSizes) {
                        int index = 0;

                        for (int i = 0; i < laundryItems.size(); i++) {
                            if(laundryItems.get(i).getPantDesign().getModelSize().equals(modelSize))
                                break;

                            index++;

                        }
                        if(index != laundryItems.size()){
                            row.add(laundryItems.get(index).getFailQuantity().toString()+"*"+laundryItems.get(index).getFailUnitPrice()+"="+laundryItems.get(index).getFailTotalPrice());
                            totalQuantitiesHor.set(index, totalQuantitiesHor.get(index)+laundryItems.get(index).getFailTotalPrice());
                            rowTotal.add(indexModelSize+2 , laundryItems.get(index).getFailTotalPrice().toString());
                            rowTotal.add(rowTotal.size()-1, laundryItems.get(index).getFailTotalPrice().toString());
                        }else{
                            row.add("0");

                        }
                        indexModelSize++;
                    }
                    row.add(laundryRequestItem.getFailTotalPrice().toString());
                    int lastPos = modelSizes.size();
                    totalQuantitiesHor.set(lastPos, totalQuantitiesHor.get(lastPos)+laundryRequestItem.getFailTotalPrice());
                    body.add(row);
                    row = new BodyRow();

                }

                body.add(rowTotal);
                System.out.println("body size: "+body.size());
                correct = true;

            } else {
                System.out.println("Laundry Request Items is empty");
            }
        } else {
            System.out.println("Laundry Request Items is null");
            correct = false;
        }
    }
}
